package cn.enited.devtools.apiaggregator.utils;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.IdUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class IdUtils {
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private static long workerId;//为终端ID
    private static long dataCenterId;//数据中心ID
    private static Snowflake snowflake = IdUtil.getSnowflake(workerId, dataCenterId);

    static  {
        dataCenterId = IdUtil.getDataCenterId(10L);
        workerId = NetUtil.ipv4ToLong(NetUtil.getLocalhostStr());
        log.info("当前机器的workId:{}",workerId);
    }

    public synchronized static long snowflakeId() {
        return snowflake.nextId();
    }
}
